package com.ruoyi.common.tools.excel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.ruoyi.common.core.service.DictService;
import com.ruoyi.common.tools.constant.ERecordConstant;
import com.ruoyi.common.tools.excel.base.MyExcelListener;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.standingbook.maintenance.domain.vo.MaintenanceImportDetailVo;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.ArrayList;
import java.util.List;

public class MaintenanceExcelListener extends MyExcelListener<MaintenanceImportDetailVo> {

	private DictService dictService = SpringUtils.getBean(DictService.class);

	private String batchNo = null;

	public MaintenanceExcelListener(String batchNo) {
		this.batchNo = batchNo;
	}

	@Override
	public void doValidate(MaintenanceImportDetailVo data, AnalysisContext context) {
		// 1.设置批次号
		// 2.校验数据 与 匹配数据字典数据 设置对应字段
		// 3.设置 处理状态 与 处理信息
		List<String> validateList = new ArrayList<>();

		data.setBatchNo(this.batchNo);

		if (!StringUtils.isEmpty(data.getServiceTypeName())) {
			int val = NumberUtils
					.toInt(this.dictService.getDictValue("erecord_service_type", data.getServiceTypeName()), -1);
			if (val == -1) {
				validateList.add("[ 服务种类 ] 匹配不到数据");
			} else {
				data.setServiceType(val);
			}
		} else {
			validateList.add("[ 服务种类 ] 为空");
		}

		Integer col = this.getHeadColIndex("现场完成前照片1");
		if (null != col) {
			data.setPic1(this.getPicOssIdOnMap(row, col));
		}

        col = this.getHeadColIndex("现场完成前照片2");
        if (null != col) {
            data.setPic2(this.getPicOssIdOnMap(row, col));
        }

		col = this.getHeadColIndex("现场完成后照片1");
		if (null != col) {
			data.setPic3(this.getPicOssIdOnMap(row, col));
		}

        col = this.getHeadColIndex("现场完成后照片2");
        if (null != col) {
            data.setPic4(this.getPicOssIdOnMap(row, col));
        }

		if (!validateList.isEmpty()) {
			String errMsg = String.join(" ，", validateList);
			excelResult.getValidateList().add(errMsg);

			data.setProcessMsg(errMsg);
			data.setProcessStatus(ERecordConstant.COMMON_STATUS_ERROR);
		} else {
			data.setProcessMsg("成功");
			data.setProcessStatus(ERecordConstant.COMMON_STATUS_SUCCESS);
		}
	}

	@Override
	protected void doOnExceptionProcess(String errorMsg) {
        MaintenanceImportDetailVo vo = new MaintenanceImportDetailVo();
		vo.setBatchNo(this.batchNo);
		vo.setProcessStatus(ERecordConstant.COMMON_STATUS_ERROR);
		vo.setProcessMsg(errorMsg);
		this.excelResult.getList().add(vo);
	}

}
